package com.xlzcx.xltz.service.a_user;


import com.xlzcx.xltz.dao.AUserMapper;
import com.xlzcx.xltz.entity.AUser;
import com.xlzcx.xltz.entity.vo.AUserVo;
import com.xlzcx.xltz.utils.IpUtil;
import com.xlzcx.xltz.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 作者: 李咨辰
 * 文件名: AUserServiceImpl.java
 * 路径: com.xlzcx.xltz.service.user
 * 时间: 2024-10-05 11:29
 * 版本: 1.0
 * 描述: TODO 用户
 **/
@Service
public class AUserServiceImpl implements AUserService {

    @Resource
    private AUserMapper aUserMapper;


    /**
     * 登录方法
     *
     * @param map 账号、密码
     * @return 登录对象
     */
    @Override
    public AUserVo doLogin(HashMap<String, String> map, HttpServletRequest request) {

        AUserVo aUser = null;
        if (Pattern.matches("^1[3-9]\\d{9}$", map.get("name"))) {
            //手机号
            aUser = aUserMapper.getUserByPhoneAndPassword(map.get("name"), map.get("password"));
        } else if (Pattern.matches("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", map.get("name"))) {
            //邮箱
            aUser = aUserMapper.getUserByEmailAndPassword(map.get("name"), map.get("password"));
        } else {
            //账号
            aUser = aUserMapper.getUserByAccountAndPassword(map.get("name"), map.get("password"));
        }

        if (aUser == null) {
            return null;
        }

        String ip = IpUtil.getIpByHttpServletRequest(request);
        //登录秘钥
        String secret = UUID.randomUUID().toString();
        aUserMapper.setSecret(aUser.getId(), secret, ip);
        aUser.setSecret(secret);
        aUser.setUpdateIp(ip);

        return aUser;
    }

    /**
     * 登录状态验证
     *
     * @param id     用户编号
     * @param secret 授权码
     * @return 登录对象
     */
    @Override
    public AUserVo loginStatus(String id, String secret, HttpServletRequest request) {
        AUserVo aUser = aUserMapper.getUserByIpAndSecret(id, secret);
        if (aUser != null) {
            String ip = IpUtil.getIpByHttpServletRequest(request);

            secret = UUID.randomUUID().toString();
            aUserMapper.setSecret(aUser.getId(), secret, ip);
            aUser.setSecret(secret);
            aUser.setUpdateIp(ip);
            return aUser;
        }
        return null;
    }

    /**
     * 判断用户是否注册
     *
     * @param user 手机号或邮箱
     * @return 没有注册返回false
     */
    @Override
    public boolean getUserByPhoneOrEmail(String user) {
        AUserVo aUser = null;
        if (Pattern.matches("^1[3-9]\\d{9}$", user)) {
            aUser = aUserMapper.getUserByPhone(user);
        } else {
            aUser = aUserMapper.getUserByEmail(user);
        }
        return aUser == null;
    }

    /**
     * 注册账号
     *
     * @param user     手机号或邮箱
     * @param password 密码
     * @param request  用户获取用户ip
     * @return 成功大于0
     */
    @Override
    public int singIn(String user, String password, HttpServletRequest request) {
        AUserVo aUser = new AUserVo();
        if (Pattern.matches("^1[3-9]\\d{9}$", user)) {
            aUser.setPhone(user);
        } else {
            aUser.setEmail(user);
        }

        //生成id
        long accountAndId = SnowFlakeUtil.getFlowIdInstance().nextId();
        AUserVo getUserById = aUserMapper.getUserById(accountAndId);

        //查询id是否重复，重复则重新生成
        while (getUserById != null) {
            accountAndId = SnowFlakeUtil.getFlowIdInstance().nextId();
            getUserById = aUserMapper.getUserById(accountAndId);
        }

        aUser.setId(accountAndId);
        aUser.setAccount(String.valueOf(accountAndId));
        aUser.setName("网友-" + (int) ((Math.random() * 900000) + 100000));

        aUser.setCreateTime(new Date());
        aUser.setCreateIp(IpUtil.getIpByHttpServletRequest(request));
        aUser.setPassword(password);

        return aUserMapper.addUser(aUser);
    }


    /**
     * 修改用户
     *
     * @param aUser   用户类
     * @param request 用于获取ip
     * @return 成功返回true
     */
    @Override
    public boolean editUser(AUserVo aUser, HttpServletRequest request) {
        aUser.setUpdateTime(new Date());
        aUser.setUpdateIp(IpUtil.getIpByHttpServletRequest(request));
        return aUserMapper.updateUser(aUser) > 0;
    }
}
